<template>
  <div>
    <el-dialog
      v-loading.lock="loading"
      class="base-dialog system-user-dialog"
      :append-to-body="true"
      title="文件夹管理信息"
      align="center"
      :close-on-click-modal="false"
      :visible.sync="innerDialogFormVisible"
    >
      <el-form
        ref="formData"
        class="base-pipeline-layout system-user-form"
        label-width="100px"
        :model="formData"
        :rules="formDataRules"
        status-icon
      >
        <el-form-item
          label="上级文件夹"
          :label-width="formLabelWidth"
          prop="parentId"
          :error="errorMsg.parentId"
        >
          <el-select v-model="formData.parentId" placeholder="请选择">
            <el-option
              v-for="item in options"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item
          label="文件夹名称"
          :label-width="formLabelWidth"
          prop="name"
          :error="errorMsg.name"
        >
          <el-input v-model="formData.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item
          label="文件夹标识"
          :label-width="formLabelWidth"
          prop="tag"
          :error="errorMsg.tag"
        >
          <el-input
            v-model="formData.tag"
            autocomplete="off"
            placeholder="请输入英文字母"
            :validate-event="false"
            @blur="validateTag"
          ></el-input>
        </el-form-item>
        <el-form-item
          label="排序"
          :label-width="formLabelWidth"
          prop="sort"
          :error="errorMsg.sort"
        >
          <el-input
            v-model="formData.sort"
            autocomplete="off"
            placeholder="请输入1-999数字"
            :validate-event="false"
            @blur="validateSort"
          ></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer" align="center">
        <el-button type="primary" @click="onSubmit('formData')"
          >保 存</el-button
        >
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  GetListNoPage,
  create,
  update,
  get,
} from "../../../api/Archive/ArchiveDic";

export default {
  name: "ArchiveDicEdit",
  props: {
    dialogFormVisible: {
      type: Boolean,
      default: false,
    },
    operateType: {
      type: String,
      default: "add",
    },
    parentId: {
      type: String,
      default: "0",
    },
    id: {
      type: String,
      default: "",
    },
  },
  data() {
    return {
      loading: false,
      innerDialogFormVisible: false,
      formLabelWidth: "100px",
      options: [],
      initData: {
        id: 0,
        parentId: "0",
        name: "",
        tag: "",
        sort: null,
      },
      formData: {
        id: 0,
        parentId: this.parentId,
        name: "",
        tag: "",
        sort: null,
      },
      formDataRules: {
        parentId: [
          {
            required: true,
            trigger: "blur",
          },
        ],
        name: [
          {
            required: true,
            trigger: "blur",
            validator: this.Helper.validateFunction,
          },
        ],
        tag: [
          {
            required: true,
            trigger: "blur",
            validator: this.Helper.validateFunction,
          },
        ],
        sort: [
          {
            required: true,
            trigger: "blur",
            validator: this.Helper.validateFunction,
          },
        ],
      },
      initErrorMsg: {
        parentId: "",
        name: "",
        tag: "",
        sort: "",
      },
      errorMsg: {
        parentId: "",
        name: "",
        tag: "",
        sort: "",
      },
    };
  },
  watch: {
    innerDialogFormVisible(val) {
      this.$emit("update:dialogFormVisible", val);
    },
  },
  created() {
    this.innerDialogFormVisible = this.dialogFormVisible;
    this.init();
  },
  methods: {
    init() {
      GetListNoPage({ parentId: 0 }).then((res) => {
        this.options = res.data;
        this.options.unshift({ name: "根目录", id: "0" });
      });
      if (this.id && this.operateType !== "add") {
        get({ id: this.id }).then((res) => {
          this.formData = res.data;
        });
      }
    },
    cancel() {
      this.innerDialogFormVisible = false;
    },
    closeDialog() {
      this.$parent.getTree();
      this.cancel();
    },
    onSubmit() {
      this.$refs.formData.validate((valid) => {
        if (valid) {
          this.loading = true;
          this.errorMsg = this.Helper.deepCopy(this.initErrorMsg);
          if (this.formData.id <= 0 && this.operateType === "add") {
            create(this.formData)
              .then((res) => {
                if (res.code === 200) {
                  this.closeDialog();
                } else {
                  this.showValidateMsgCheck(res);
                }
                this.loading = false;
              })
              .catch(() => {
                this.loading = false;
              });
          } else {
            update(this.formData)
              .then((res) => {
                if (res.code === 200) {
                  this.closeDialog();
                } else {
                  this.showValidateMsgCheck(res);
                }
                this.loading = false;
              })
              .catch(() => {
                this.loading = false;
              });
          }
        } else {
          return false;
        }
      });
    },
    showValidateMsgCheck(res) {
      if (res.msg) {
        this.Helper.showValidateMsg("parentId", this.errorMsg, res.msg);
        this.Helper.showValidateMsg("name", this.errorMsg, res.msg);
        this.Helper.showValidateMsg("tag", this.errorMsg, res.msg);
        this.Helper.showValidateMsg("sort", this.errorMsg, res.msg);
      }
    },
    validateTag() {
      if (!/^[A-Za-z]+$/.test(this.formData.tag)) {
        this.errorMsg.tag = "请输入英文字母";
        this.formData.tag = "";
      } else {
        this.errorMsg.tag = "";
      }
    },
    validateSort() {
      if (!/^[1-9]\d{0,2}$/.test(this.formData.sort)) {
        this.errorMsg.sort = "请输入1-999数字";
        this.formData.sort = "";
      } else {
        this.errorMsg.sort = "";
      }
    },
  },
};
</script>
